<?php
namespace Modules\Activity\Entities\Lottery;;

use Modules\Core\Entities\BaseEntity as Model;
use ThrowException;

use Modules\Order\Traits\Skuable;

use Modules\Asset\Entities\PackageSku;

// 一番赏活动 副表
class Sku extends Model
{
    use Skuable;
    
    protected $table = 'lottery_skus';

   	protected $guarded = [];
    static public $resourcename = '抽奖奖品';

    protected $casts = [
        'assets' => 'json'
    ];

    public function activity()
    {
        return $this->belongsTo('\Modules\Activity\Entities\Lottery\Activity', 'activity_id');
    }

    // public function getType($type) 
    // {
    //     $map = [
    //         'base_sku_type' => 'lottery',
    //         'node_type' => 'lottery'
    //     ];

    //     return @$map[$type];
    // }

    // // 
    // public function getNodeEntity()
    // {
    //     return $this;
    // }


    // 获取列表
    static public function beforeGetList($options) 
    {
    	return $options;
    }

    // 送给用户
    public function sendToUser($user, $activity, $ticket)
    {
        $sku = $this;
        $item = PackageSku::create([
            'uuid' => uniqid(),
            'user_id' => $user->id,
            'title' => $sku->title,
            'number' => $sku->number,
            'thumb' => $sku->thumb,
            'sku_type' => $sku->sku_type,
            'product_id' => $sku->product_id,
            'assets' => $sku->assets,
            'total' => 1,
            'score_price' => $sku->getSellingPrice('score'),
            'money_price' => $sku->getSellingPrice('money'),
            'pay_order_id' => null,
            'activity_id' => null,
            'activity_type' => null,
            'activity_uuid' => null,
            'node_id' => $activity->id,
            'node_type' => $activity->getType('node_type'),
            'node_uuid' => $activity->uuid,
            'node_title' => $activity->title,
            'open_indexs' => null,
            'open_index' => null,
            'options' => [
                'node_title' => $activity->title,
                'session' => $activity->current_session,
                'ticket_number' => $ticket->number
            ],
            'source_type' => 3,     // 开盒
            'room_id' => null
        ]);

        // 尝试自动领取虚拟资产
        $item->tryAutoPick();

        return $item;
    }
}
